package com.example.shoushijiance;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {
    Animation[] animation=new Animation[4];
int distean=50;
ViewFlipper flipper;
private int[] images=new int[]{
        R.mipmap.timg,R.mipmap.timg1,R.mipmap.timg17,
        R.mipmap.timg18,R.mipmap.timg19,R.mipmap.timg2,
        R.mipmap.wangyu
};
GestureDetector detector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        detector=new GestureDetector(MainActivity.this,this);
        flipper=findViewById(R.id.flipper);
        for (int i=0;i<images.length;i++)
        {
            ImageView imageView=new ImageView(this);
            imageView.setImageResource(images[i]);
            flipper.addView(imageView);
        }
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (e1.getX()-e2.getX()>distean) {
            flipper.showNext();
            Toast.makeText(MainActivity.this, "向左划", Toast.LENGTH_SHORT).show();
            return true;
        }
        else if (e2.getX()-e1.getX()>distean) {
            Toast.makeText(MainActivity.this, "向右划", Toast.LENGTH_SHORT).show();
            flipper.showPrevious();
            return true;
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        return detector.onTouchEvent(event);
    }
}